<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/12
 * Time: 16:30
 */

namespace core\request;


use core\Config;

class Request
{
    private static $instance;
    private $request_method;
    private $module;
    private $controller;
    private $action;
    protected $class_full_path;


    public function __construct()
    {
        if (isset($_SERVER['REDIRECT_URL'])){
            $req = explode('/',substr($_SERVER['REDIRECT_URL'],1));
        }else{
            $req = [];
        }
        $req = array_filter($req);
        $this->initAccessPath($req);
        $config = Config::getInstance()->getConfig('app');
        define('__APP__',__ROOT_PATH__.$config['app_path'].'/');
        if (is_dir(__APP__.ucfirst($this->module).'/controller/')){
            define('__MODULE__',__APP__.ucfirst($this->module).'/controller/');
            if (is_file(__MODULE__.ucfirst($this->controller).'.php')){
                define('__CLASS_FULL_PATH__',__METHOD__.ucfirst($this->controller).'.php');
                if(is_callable(['app\\'.$this->module.'\\controller\\'.$this->controller,$this->action])){
                    $this->class_full_path = 'app\\'.$this->module.'\\controller\\'.$this->controller;
                }else{
                    header('HTTP/1.1 404 Not Found');
                    echo '404 not found';
                }
            }else{
                header('HTTP/1.1 404 Not Found');
                echo '404 not found';
            }
        }else{
            header('HTTP/1.1 404 Not Found');
            echo '404 not found';
        }
    }
    /**
     * @return static
     */
    public static function getInstance()
    {
        if (is_null(static::$instance)){
            static::$instance = new static();
        }
        return static::$instance;
    }

    public function run()
    {
        $con = $this->class_full_path;
        if (class_exists($con)) {
            $sss = new $con();
            call_user_func([$sss, $this->action]);
        }else{
            header('HTTP/1.1 404 Not Found');
            echo '404 not found';
        }
    }

    /**
     * 初始化访问路径
     * @param $req
     */
    public function initAccessPath($req)
    {
        $config = Config::getInstance()->getConfig('app');
        switch (count($req)){
            case 1:list($this->module) = $req;
                $this->controller=$config['default_controller'];
                $this->action=$config['default_action'];
                break;
            case 2:
                list($this->module,$this->controller) = $req;
                $this->action=$config['default_action'];
                break;
            case 3:
                list($this->module,$this->controller,$this->action) = $req;
                break;
            case 0:
                $this->module = $config['default_module'];
                $this->controller = $config['default_controller'];
                $this->action = $config['default_action'];
                break;
            default:
                list($this->module,$this->controller,$this->action) = $req;
                break;
        }
    }

    /**
     * @return mixed
     */
    public function getModule()
    {
        return $this->module;
    }

    /**
     * @return mixed
     */
    public function getController()
    {
        return $this->controller;
    }

    /**
     * @return mixed
     */
    public function getAction()
    {
        return $this->action;
    }

    public function getClassFullPath()
    {
        return $this->class_full_path;
    }

    /**
     * 是否POST请求
     * @return bool
     */
    public function is_post()
    {
        if (strtolower($this->request_method) == 'post'){
            return true;
        }
        return false;
    }

    /**
     * 是否get请求
     * @return bool
     */
    public function is_get()
    {
        if (strtolower($this->request_method) == 'get'){
            return true;
        }
        return false;
    }

    public function is_ajax()
    {
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
            return true;
        }
        return false;
    }
}